package org.geppeto.eva.notes;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.geppeto.eva.eventory.Repository;

//import org.json.JSONArray;
import java.lang.String;
import java.net.URLDecoder;
import java.io.IOException;
import java.util.Hashtable;
import java.util.UUID;

/**
 * @author 
 * Marko Horvat,
 * Miroslav Popovic, popovic.miroslav@gmail.com
 * 
 */
public class NotesREST extends HttpServlet {
	
	public static String Q_OPERATION = "operation";
	public static String VALUE_SAVE = "save";
	public static String VALUE_REFRESH = "refresh";
	public static String VALUE_RESET = "reset";
	
	public static String Q_NOTE = "note";
	
	public static String Q_RESOURCE = "name";
	
	public NotesREST(){
	}	

	//MATIJA - pogledaj InstanceManager
	private FileManager fMngr = null;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		try {
			String result="";
			fMngr = FileManager.getFileManager();			

			//TODO --> Transfer this part to doPOST function after testing

			//Create instance
			String operation = req.getParameter(Q_OPERATION);
			String msgText = req.getParameter(Q_NOTE);
			String fileName = req.getParameter(Q_RESOURCE);
			
//			if (!isValidMechanism(mechanism)){
//				respondToClient(resp, "You specified an invaild mechanism!");
//				return;
//			}
//			if (!isValidOperation(mechanism, operation)){
//				respondToClient(resp, "You specified an invaild operation on the mechanism!");
//				return;
//			}
//				
			
			if (operation.equals(VALUE_SAVE)){				
				synchronized(fMngr){
					result = fMngr.saveToFile(msgText, fileName);					
				}
				respondToClient(resp, result);
				return;
			} else //Delete instance			
			if (operation.equals(VALUE_REFRESH)){				
				synchronized(fMngr){
					result = fMngr.readFile(fileName);					
				}
				respondToClient(resp, result);
				return;
			} else
			if (operation.equals(VALUE_RESET)){				
				synchronized(fMngr){
					result = fMngr.saveToFile("0", fileName);					
				}
				respondToClient(resp, result);
				return;
			}

		} catch (Exception err){
			respondToClient(resp, err.getMessage());
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		try {

		} catch (Exception err){
			respondToClient(resp, err.getMessage());
		}
	}

	
	private void respondToClient(HttpServletResponse resp, String response) throws IOException {
		try {
			resp.getWriter().print(response);
		} catch (Exception err) {
			resp.getWriter().print(err.getMessage());
		}
	};

	private boolean isValidMechanism(String mechanism){
		boolean result =  false;
		String [] listOfMechanisms = {"semaphore", "mailbox"};
		for (int i = 0; i < listOfMechanisms.length; i++){
			if (mechanism.equals(listOfMechanisms[i])){
				result = true;
				break;
			}
		}		
		return result;
	}

	private boolean isValidOperation(String mechanism, String operation){
		boolean result =  false;
		Hashtable mech_operations = new Hashtable();		
		mech_operations.put("semaphore", new String[] {"create", "delete", "acquire", "release"});
		mech_operations.put("mailbox", new String[] {"create", "delete", "getMsg", "putMsg"});
		try {
			String [] oper = (String []) mech_operations.get(mechanism);
			for (int i = 0; i < oper.length; i++){
				if (operation.equals(oper[i])){
					result = true;
					break;
				}
			}			
			return result;
		} catch (Exception ex) {
			return false;
		}
	}
	
}
